package com.cooldatasoft.domain.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooldatasoft.domain.dao.TaskDao;
import com.cooldatasoft.domain.dao.UserDao;
import com.cooldatasoft.domain.model.Project;
import com.cooldatasoft.domain.model.Task;
import com.cooldatasoft.domain.model.User;
import com.cooldatasoft.domain.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDao userDao;

	@Autowired
	private TaskDao taskDao;
	
	public Long insertUser(User user) {
		return userDao.insert(user);
	}

	public void updateUser(User user) {
		userDao.updateUser(user);
	} 

	public void deleteUser(Long userId) {
		userDao.delete(userId);
	}

	public List<User> getAllUser() {
		return userDao.getAll();
	}

	@Override
	public User getUserByEmail(String email) {
		return userDao.getUserByEmail(email);
	}

	@Override
	public User getUserById(Long id) {
		return userDao.getUserById(id);
	}

	@Override
	public User getUserBySurname(String surname) {
		return userDao.getUserBySurname(surname);
	}

	@Override
	public User getUserByPassword(String openId) {
		return userDao.getUserByPassword(openId);
	}

	@Override
	public int getTaskPercentage(User user_in, Project project_in) {
		
		List<Task> allTask = taskDao.getTasksToProject(project_in);
		List<Task> userTask = taskDao.getTasksOnProjectWithUser(user_in, project_in);
		
		List<Task> doneTasks = new ArrayList<Task>();
		for (Task task : userTask) {
			if (task.getTaskStatus().equals("done")) {
				doneTasks.add(task);
			}
		}
		
		return (int) (((float) doneTasks.size() / allTask.size()) * 100);
	}
}
